package cn.gson.crm.controller.classroom;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.ClassroomDao;
import cn.gson.crm.model.entity.Classroom;

/**
 * 创建班级
 */
@WebServlet("/create/classroom")
public class CreateClassroom extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private ClassroomDao crd = new ClassroomDao();
    
	@Override
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.get(request, response);
		forward("class/create_classroom.jsp");
	}
	@Override
	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.post(request, response);
		String cid = request.getParameter("cid");
		String className = request.getParameter("className");
		String classManager = request.getParameter("classManager");
		String classTeacher = request.getParameter("classTeacher");
		String classType = request.getParameter("classType");
		System.out.println(classType);
		
		if(className == null || className.isEmpty()){
			request.setAttribute("erro", "班级不能为空!");
			get(request, response);
			return;
		}
		if(classManager == null || classManager.isEmpty()){
			request.setAttribute("erro", "经理不能为空!");
			get(request, response);
			return;
		}
		if(classTeacher == null || classTeacher.isEmpty()){
			request.setAttribute("erro", "班主任不能为空!");
			get(request, response);
			return;
		}
		if(classType == null || classType.isEmpty()){
			request.setAttribute("erro", "班级类型不能为空!");
			get(request, response);
			return;
		}
		
		Classroom cr = new Classroom();
		cr.setClassName(className);
		cr.setClassManager(classManager);
		cr.setClassTeacher(classTeacher);
		cr.setClassType(Integer.parseInt(classType));
		
		if(crd.classroomSave(cr)){
			redirect("class/all");
			return;
		}
		request.setAttribute("erro", "保存失败!");
		get(request, response);
		
	}
}
